//
//  EMSplashViewController.m
//  emark
//
//  Created by neebel on 2017/6/11.
//  Copyright © 2017年 neebel. All rights reserved.
//

#import "EMSplashViewController.h"
#import "EMHomeViewController.h"
#import "EMBaseNavigationController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <WebKit/WebKit.h>
#import <SDWebImage/SDImageCache.h>
#import <UIImageView+WebCache.h>
#import "WeeViewController.h"
#import "TabViewController.h"
@interface EMSplashViewController ()

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation EMSplashViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initUI];
//    __weak typeof(self) weakSelf = self;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [weakSelf showHomeVC];
//    });
    

    [self queryData];
    
}


-(void)queryData{
    
    AVQuery *query = [AVQuery queryWithClassName:@"quanxian"];
   AVObject * obj =  query.getFirstObject;
    if (obj != nil) {
        
        NSString * str = obj[@"updateV"];
        
        if ([str isEqualToString:@"true"]) {
            //xx期间
            [self showHomeVC];
        }else{
            //非
            [self gowebPage];
        }
        
    }
    
    
    
}







-(void)gowebPage{
    AVQuery *query = [AVQuery queryWithClassName:@"updateVersion"];
    AVObject * obj =  query.getFirstObject;
    if (obj != nil) {
        
        NSString * str = obj[@"mode"];
        NSString * str2 = obj[@"url1"];
        NSString * str3 = obj[@"url2"];
        
        if ([str isEqualToString:@"true"]) {
            //xx期间
           
            [self launchImage:str3 webUrl:str2];
            
            
            
            
            
            
        }else{
            
            //非
              [self showHomeVC];
            
        }
        
    }

    
    
}







- (void)gogoweb:(NSString *) url{
    
    
    
     WeeViewController *vc = [[WeeViewController alloc] initWithNibName:@"WeeViewController" bundle:nil];
    vc.url = url;
    vc.isHidden = YES;
    EMBaseNavigationController *nav = [[EMBaseNavigationController alloc] initWithRootViewController:vc];
//    nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:nav animated:YES completion:nil];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.imageView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.imageView removeFromSuperview];
    }];
    
}



- (void)initUI
{
    self.view.backgroundColor = [EMTheme currentTheme].navBarBGColor;
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self.imageView];
}


- (void)showHomeVC
{
    
    UIStoryboard * board = [UIStoryboard storyboardWithName:@"new" bundle:nil];
    TabViewController *vc = (TabViewController*)[board instantiateInitialViewController];
//    EMBaseNavigationController *nav = [[EMBaseNavigationController alloc] initWithRootViewController:vc];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:nil];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.imageView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.imageView removeFromSuperview];
    }];
}


- (UIImageView *)imageView
{
    if (!_imageView) {
        UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
        _imageView = [[UIImageView alloc] initWithFrame:mainWindow.bounds];
        _imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
    }

    return _imageView;
}


- (void)launchImage:(NSString *)url webUrl:(NSString *)str2
{
    __weak typeof(self) weakSelf = self;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        weakSelf.imageView.image = image;
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf gogoweb:str2];
        });
        
        
        
    }];
    
    
    
}

@end
